package com.woniuxy.company5.security;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ：fengSir
 * @date ：Created By 2022-12-01 15:22
 * @description ：TODO
 */
public class LoginFilter extends AbstractAuthenticationProcessingFilter {
    //定义过滤器拦截的请求映射
    private final static String matchUrl = "/users/login";
    public LoginFilter() {
        super(new AntPathRequestMatcher(matchUrl));
    }
    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(request.getParameter("account"), request.getParameter("password"));
        return this.getAuthenticationManager().authenticate(authenticationToken);//开启认证,接下来走认证框架
    }
}
